var WILDCARD = '*';


function FlowableModdleExtension(eventBus) {

    var self = this;

    eventBus.on('moddleCopy.canCopyProperty', function(context) {
        var property = context.property,
            parent = context.parent;

        return canCopyProperty(property, parent);
    });


    function canCopyProperty (property, parent) {

        // (1) check wether property is allowed in parent
        if (isObject(property) && !isAllowedInParent(property, parent)) {

            return false;
        }

        // (2) check more complex scenarios

        if (is(property, 'flowable:InputOutput') && !this.canHostInputOutput(parent)) {
            return false;
        }

        if (isAny(property, [ 'flowable:Connector', 'flowable:Field' ]) && !this.canHostConnector(parent)) {
            return false;
        }

    }

    function canHostInputOutput(parent) {

        // allowed in flowable:Connector
        var connector = getParent(parent, 'flowable:Connector');

        if (connector) {
            return true;
        }

        // special rules inside bpmn:FlowNode
        var flowNode = getParent(parent, 'bpmn:FlowNode');

        if (!flowNode) {
            return false;
        }

        if (isAny(flowNode, [ 'bpmn:StartEvent', 'bpmn:Gateway', 'bpmn:BoundaryEvent' ])) {
            return false;
        }

        if (is(flowNode, 'bpmn:SubProcess') && flowNode.get('triggeredByEvent')) {
            return false;
        }

        return true;
    }

    function canHostConnector (parent) {

        var serviceTaskLike = getParent(parent, 'flowable:ServiceTaskLike');

        if (is(serviceTaskLike, 'bpmn:MessageEventDefinition')) {

            // only allow on throw and end events
            return (
                getParent(parent, 'bpmn:IntermediateThrowEvent') ||
                getParent(parent, 'bpmn:EndEvent')
            );
        }

        return true;
    }
}



// /**
//  * Check wether to disallow copying property.
//  */
// FlowableModdleExtension.prototype.canCopyProperty = function(property, parent) {
//
//     // (1) check wether property is allowed in parent
//     if (isObject(property) && !isAllowedInParent(property, parent)) {
//
//         return false;
//     }
//
//     // (2) check more complex scenarios
//
//     if (is(property, 'flowable:InputOutput') && !this.canHostInputOutput(parent)) {
//         return false;
//     }
//
//     if (isAny(property, [ 'flowable:Connector', 'flowable:Field' ]) && !this.canHostConnector(parent)) {
//         return false;
//     }
//
// };

// FlowableModdleExtension.prototype.canHostInputOutput = function(parent) {
//
//     // allowed in flowable:Connector
//     var connector = getParent(parent, 'flowable:Connector');
//
//     if (connector) {
//         return true;
//     }
//
//     // special rules inside bpmn:FlowNode
//     var flowNode = getParent(parent, 'bpmn:FlowNode');
//
//     if (!flowNode) {
//         return false;
//     }
//
//     if (isAny(flowNode, [ 'bpmn:StartEvent', 'bpmn:Gateway', 'bpmn:BoundaryEvent' ])) {
//         return false;
//     }
//
//     if (is(flowNode, 'bpmn:SubProcess') && flowNode.get('triggeredByEvent')) {
//         return false;
//     }
//
//     return true;
// };

// FlowableModdleExtension.prototype.canHostConnector = function(parent) {
//
//     var serviceTaskLike = getParent(parent, 'flowable:ServiceTaskLike');
//
//     if (is(serviceTaskLike, 'bpmn:MessageEventDefinition')) {
//
//         // only allow on throw and end events
//         return (
//             getParent(parent, 'bpmn:IntermediateThrowEvent') ||
//             getParent(parent, 'bpmn:EndEvent')
//         );
//     }
//
//     return true;
// };



// helpers //////////

function is(element, type) {
    return element && isFunction(element.$instanceOf) && element.$instanceOf(type);
}

function isAny(element, types) {
    return some(types, function(t) {
        return is(element, t);
    });
}


function isAllowedInParent(property, parent) {

    // (1) find property descriptor
    var descriptor = property.$type && property.$model.getTypeDescriptor(property.$type);

    var allowedIn = descriptor && descriptor.meta && descriptor.meta.allowedIn;

    if (!allowedIn || isWildcard(allowedIn)) {
        return true;
    }

    // (2) check wether property has parent of allowed type
    return some(allowedIn, function(type) {
        return getParent(parent, type);
    });
}

function isWildcard(allowedIn) {
    return allowedIn.indexOf(WILDCARD) !== -1;
}


FlowableModdleExtension.$inject = [ 'eventBus' ];

var flowableExtensionModule = {
    __init__: [ 'flowableModdleExtension' ],
    flowableModdleExtension: [ 'type', FlowableModdleExtension ]
};

